/**
 * MIT License
 * Copyright (c) 2018 yadong.zhang
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Data;
import com.zyd.shiro.business.entity.Dept;
import com.zyd.shiro.business.service.SysDataService;
import com.zyd.shiro.business.service.SysDeptService;
import com.zyd.shiro.business.vo.DeptConditionVO;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.SysData;
import com.zyd.shiro.persistence.beans.SysDept;
import com.zyd.shiro.persistence.mapper.SysDataMapper;
import com.zyd.shiro.persistence.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Slf4j
@Service
public class SysDataServiceImpl implements SysDataService {

    @Autowired
    private SysDataMapper sysDataMapper;



    @Override
    public Data insert(Data data) {
        Assert.notNull(data, "数据权限不可为空！");
        data.setUpdateTime(new Date());
        data.setCreateTime(new Date());
        sysDataMapper.insertSelective(data.getSysData());
        return data;
    }




    @Override
    public void insertList(List<Data> entities) {

    }

    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return sysDataMapper.deleteByPrimaryKey(primaryKey) > 0;
    }



    @Override
    public boolean updateSelective(Data data) {
        Assert.notNull(data, "数据权限不可为空！");
        data.setUpdateTime(new Date());
        return sysDataMapper.updateByPrimaryKeySelective(data.getSysData()) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Data data) {
        Assert.notNull(data, "数据权限不可为空！");
        data.setUpdateTime(new Date());

        return sysDataMapper.updateByPrimaryKey(data.getSysData()) > 0;
    }



    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Data getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "数据权限不可为空！");
        SysData sysData = sysDataMapper.selectByPrimaryKey(primaryKey);
        return null == sysData ? null : new Data(sysData);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param data
     * @return
     */
    @Override
    public Data getOneByEntity(Data data) {
        Assert.notNull(data, "数据权限不可为空！");
        SysData sysData = sysDataMapper.selectOne(data.getSysData());
        return null == sysData ? null : new Data(sysData);
    }

    @Override
    public List<Data> listAll() {
        List<SysData> sysDatas = sysDataMapper.selectAll();

        if (CollectionUtils.isEmpty(sysDatas)) {
            return null;
        }
        List<Data> datas = new ArrayList<>();
        for (SysData sysData : sysDatas) {
            datas.add(new Data(sysData));
        }
        return datas;
    }


    @Override
    public List<Data> listByEntity(Data data) {
        Assert.notNull(data, "数据权限不可为空！");
        List<SysData> sysDatas = sysDataMapper.select(data.getSysData());
        if (CollectionUtils.isEmpty(sysDatas)) {
            return null;
        }
        List<Data> datas = new ArrayList<>();
        for (SysData sysData : sysDatas) {
            datas.add(new Data(sysData));
        }
        return datas;
    }

    @Override
    public Data queryDataListWithSelected(Long uid) {
        SysData sysData = sysDataMapper.listUserData(uid);
        return new Data(sysData);
    }

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Data> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysData> sysDatas = sysDataMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(sysDatas)) {
            return null;
        }
        List<Data> datas = new ArrayList<>();
        for (SysData de : sysDatas) {
            datas.add(new Data(de));
        }
        PageInfo bean = new PageInfo<SysData>(sysDatas);
        bean.setList(datas);
        return bean;
    }









}
